<!-- HTML header for doxygen 1.8.20-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=9"/>
	<meta name="generator" content="Doxygen 1.9.4"/>
	<meta name="viewport" content="width=device-width, initial-scale=1"/>
	<title>Raspberry Pi Pico SDK: pico_sha256</title>
	<!-- <link href="tabs.css" rel="stylesheet" type="text/css"/> -->
	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript" src="dynsections.js"></script>
	<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
	<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
	<link href="doxygen.css" rel="stylesheet" type="text/css" />
	<link href="normalise.css" rel="stylesheet" type="text/css"/>
<link href="main.css" rel="stylesheet" type="text/css"/>
<link href="styles.css" rel="stylesheet" type="text/css"/>
</head>
<body>
	<div class="navigation-mobile">
		<div class="logo--mobile">
			<a href="/"><img src="logo-mobile.svg" alt="Raspberry Pi"></a>
		</div>
		<div class="navigation-toggle">
			<span class="line-1"></span>
			<span class="line-2">
				<p>Menu Toggle</p>
			</span>
			<span class="line-3"></span>
		</div>
	</div>
	<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
		<div class="logo">
			<a href="index.html"> <img src="logo.svg" alt="Raspberry Pi"></a>
			<span style="display: inline-block; margin-top: 10px;">
				v2.1.0
			</span>
		</div>
		<div class="navigation-footer">
			<img src="logo-mobile.svg" alt="Raspberry Pi">
			<a href="https://www.raspberrypi.com/" target="_blank">By Raspberry Pi Ltd</a>
		</div>
<!-- 		<div class="search">
			<form>
				<input type="search" name="search" id="search" placeholder="Search">
				<input type="submit" value="Search">
			</form>
		</div> -->
<!-- Generated by Doxygen 1.9.4 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('group__pico__sha256.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="summary">
<a href="#nested-classes">Data Structures</a> &#124;
<a href="#typedef-members">Typedefs</a> &#124;
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle"><div class="title">pico_sha256<div class="ingroups"><a class="el" href="group__high__level.html">High Level APIs</a></div></div></div>
</div><!--header-->
<div class="contents">

<p>SHA-256 Hardware Accelerated implementation.  
<a href="#details">More...</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Data Structures</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="structpico__sha256__state.html">pico_sha256_state</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">SHA-256 state used by the API.  <a href="structpico__sha256__state.html#details">More...</a><br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="typedef-members" name="typedef-members"></a>
Typedefs</h2></td></tr>
<tr class="memitem:gadb68f9eb89ce1a5b795463cd083a30a0"><td class="memItemLeft" align="right" valign="top"><a id="gadb68f9eb89ce1a5b795463cd083a30a0" name="gadb68f9eb89ce1a5b795463cd083a30a0"></a>
typedef struct <a class="el" href="structpico__sha256__state.html">pico_sha256_state</a>&#160;</td><td class="memItemRight" valign="bottom"><b>pico_sha256_state_t</b></td></tr>
<tr class="memdesc:gadb68f9eb89ce1a5b795463cd083a30a0"><td class="mdescLeft">&#160;</td><td class="mdescRight">SHA-256 state used by the API. <br /></td></tr>
<tr class="separator:gadb68f9eb89ce1a5b795463cd083a30a0"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:ga56a125e51ec616f61082cc63c8599dcb"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pico__sha256.html#ga56a125e51ec616f61082cc63c8599dcb">pico_sha256_try_start</a> (<a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *state, enum <a class="el" href="group__hardware__sha256.html#gaaf3089af3667088256de5d352973d5e6">sha256_endianness</a> endianness, bool use_dma)</td></tr>
<tr class="memdesc:ga56a125e51ec616f61082cc63c8599dcb"><td class="mdescLeft">&#160;</td><td class="mdescRight">Start a SHA-256 calculation returning immediately with an error if the SHA-256 hardware is not available.  <a href="group__pico__sha256.html#ga56a125e51ec616f61082cc63c8599dcb">More...</a><br /></td></tr>
<tr class="separator:ga56a125e51ec616f61082cc63c8599dcb"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga6e6d3e0a3cb87e32a73757bcda88ebc1"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pico__sha256.html#ga6e6d3e0a3cb87e32a73757bcda88ebc1">pico_sha256_start_blocking_until</a> (<a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *state, enum <a class="el" href="group__hardware__sha256.html#gaaf3089af3667088256de5d352973d5e6">sha256_endianness</a> endianness, bool use_dma, <a class="el" href="group__timestamp.html#ga8b7f3d602cc053f05bacceeacc257ed8">absolute_time_t</a> until)</td></tr>
<tr class="memdesc:ga6e6d3e0a3cb87e32a73757bcda88ebc1"><td class="mdescLeft">&#160;</td><td class="mdescRight">Start a SHA-256 calculation waiting for a defined period for the SHA-256 hardware to be available.  <a href="group__pico__sha256.html#ga6e6d3e0a3cb87e32a73757bcda88ebc1">More...</a><br /></td></tr>
<tr class="separator:ga6e6d3e0a3cb87e32a73757bcda88ebc1"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gaf8fdc6d7438ffed9fcbc124eb134ae6e"><td class="memItemLeft" align="right" valign="top">static int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pico__sha256.html#gaf8fdc6d7438ffed9fcbc124eb134ae6e">pico_sha256_start_blocking</a> (<a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *state, enum <a class="el" href="group__hardware__sha256.html#gaaf3089af3667088256de5d352973d5e6">sha256_endianness</a> endianness, bool use_dma)</td></tr>
<tr class="memdesc:gaf8fdc6d7438ffed9fcbc124eb134ae6e"><td class="mdescLeft">&#160;</td><td class="mdescRight">Start a SHA-256 calculation, blocking forever waiting until the SHA-256 hardware is available.  <a href="group__pico__sha256.html#gaf8fdc6d7438ffed9fcbc124eb134ae6e">More...</a><br /></td></tr>
<tr class="separator:gaf8fdc6d7438ffed9fcbc124eb134ae6e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga43c112e22aea468ce0551c5078e155cd"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pico__sha256.html#ga43c112e22aea468ce0551c5078e155cd">pico_sha256_update</a> (<a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *state, const uint8_t *data, size_t data_size_bytes)</td></tr>
<tr class="memdesc:ga43c112e22aea468ce0551c5078e155cd"><td class="mdescLeft">&#160;</td><td class="mdescRight">Add byte data to be SHA-256 calculation.  <a href="group__pico__sha256.html#ga43c112e22aea468ce0551c5078e155cd">More...</a><br /></td></tr>
<tr class="separator:ga43c112e22aea468ce0551c5078e155cd"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga9bf8472019386229c3ba3b6d169b9656"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pico__sha256.html#ga9bf8472019386229c3ba3b6d169b9656">pico_sha256_update_blocking</a> (<a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *state, const uint8_t *data, size_t data_size_bytes)</td></tr>
<tr class="memdesc:ga9bf8472019386229c3ba3b6d169b9656"><td class="mdescLeft">&#160;</td><td class="mdescRight">Add byte data to be SHA-256 calculation.  <a href="group__pico__sha256.html#ga9bf8472019386229c3ba3b6d169b9656">More...</a><br /></td></tr>
<tr class="separator:ga9bf8472019386229c3ba3b6d169b9656"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga4eac54bf699dba0067a22ae3ef5ae121"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__pico__sha256.html#ga4eac54bf699dba0067a22ae3ef5ae121">pico_sha256_finish</a> (<a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *state, <a class="el" href="unionsha256__result__t.html">sha256_result_t</a> *out)</td></tr>
<tr class="memdesc:ga4eac54bf699dba0067a22ae3ef5ae121"><td class="mdescLeft">&#160;</td><td class="mdescRight">Finish the SHA-256 calculation and return the result.  <a href="group__pico__sha256.html#ga4eac54bf699dba0067a22ae3ef5ae121">More...</a><br /></td></tr>
<tr class="separator:ga4eac54bf699dba0067a22ae3ef5ae121"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<p >SHA-256 Hardware Accelerated implementation. </p>
<p >RP2350 is equipped with a hardware accelerated implementation of the SHA-256 hash algorithm. This should be much quicker than performing a SHA-256 checksum in software.</p>
<div class="fragment"><div class="line"><a class="code hl_struct" href="structpico__sha256__state.html">pico_sha256_state_t</a> state;</div>
<div class="line"><span class="keywordflow">if</span> (<a class="code hl_function" href="group__pico__sha256.html#ga56a125e51ec616f61082cc63c8599dcb">pico_sha256_try_start</a>(&amp;state, <a class="code hl_enumvalue" href="group__hardware__sha256.html#ggaaf3089af3667088256de5d352973d5e6a9ed40fa208beaabfc2fc5b8edbd652c1">SHA256_BIG_ENDIAN</a>, <span class="keyword">true</span>) == <a class="code hl_enumvalue" href="group__pico__base.html#ggaddcb20bc9a61ceff479aa87369a862aaa9b50bcb755cf2e8295de9ffa52b252fd">PICO_OK</a>) {</div>
<div class="line">    <a class="code hl_union" href="unionsha256__result__t.html">sha256_result_t</a> result;</div>
<div class="line">    <a class="code hl_function" href="group__pico__sha256.html#ga43c112e22aea468ce0551c5078e155cd">pico_sha256_update</a>(&amp;state, some_data, <span class="keyword">sizeof</span>(some_data));</div>
<div class="line">    <a class="code hl_function" href="group__pico__sha256.html#ga43c112e22aea468ce0551c5078e155cd">pico_sha256_update</a>(&amp;state, some_more_data, <span class="keyword">sizeof</span>(some_more_data));</div>
<div class="line">    <a class="code hl_function" href="group__pico__sha256.html#ga4eac54bf699dba0067a22ae3ef5ae121">pico_sha256_finish</a>(&amp;state, &amp;result);</div>
<div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code hl_define" href="group__hardware__sha256.html#ga08a18381a4c5a7c9f7d0d1b6550621a3">SHA256_RESULT_BYTES</a>; i++) {</div>
<div class="line">        printf(<span class="stringliteral">&quot;%02x&quot;</span>, result.bytes[i]);</div>
<div class="line">    }</div>
<div class="line">}</div>
<div class="ttc" id="agroup__hardware__sha256_html_ga08a18381a4c5a7c9f7d0d1b6550621a3"><div class="ttname"><a href="group__hardware__sha256.html#ga08a18381a4c5a7c9f7d0d1b6550621a3">SHA256_RESULT_BYTES</a></div><div class="ttdeci">#define SHA256_RESULT_BYTES</div><div class="ttdoc">Size of a sha256 result in bytes.</div><div class="ttdef"><b>Definition:</b> sha256.h:44</div></div>
<div class="ttc" id="agroup__hardware__sha256_html_ggaaf3089af3667088256de5d352973d5e6a9ed40fa208beaabfc2fc5b8edbd652c1"><div class="ttname"><a href="group__hardware__sha256.html#ggaaf3089af3667088256de5d352973d5e6a9ed40fa208beaabfc2fc5b8edbd652c1">SHA256_BIG_ENDIAN</a></div><div class="ttdeci">@ SHA256_BIG_ENDIAN</div><div class="ttdoc">Big Endian.</div><div class="ttdef"><b>Definition:</b> sha256.h:51</div></div>
<div class="ttc" id="agroup__pico__base_html_ggaddcb20bc9a61ceff479aa87369a862aaa9b50bcb755cf2e8295de9ffa52b252fd"><div class="ttname"><a href="group__pico__base.html#ggaddcb20bc9a61ceff479aa87369a862aaa9b50bcb755cf2e8295de9ffa52b252fd">PICO_OK</a></div><div class="ttdeci">@ PICO_OK</div><div class="ttdoc">No error; the operation succeeded.</div><div class="ttdef"><b>Definition:</b> error.h:23</div></div>
<div class="ttc" id="agroup__pico__sha256_html_ga43c112e22aea468ce0551c5078e155cd"><div class="ttname"><a href="group__pico__sha256.html#ga43c112e22aea468ce0551c5078e155cd">pico_sha256_update</a></div><div class="ttdeci">void pico_sha256_update(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes)</div><div class="ttdoc">Add byte data to be SHA-256 calculation.</div><div class="ttdef"><b>Definition:</b> sha256.c:137</div></div>
<div class="ttc" id="agroup__pico__sha256_html_ga4eac54bf699dba0067a22ae3ef5ae121"><div class="ttname"><a href="group__pico__sha256.html#ga4eac54bf699dba0067a22ae3ef5ae121">pico_sha256_finish</a></div><div class="ttdeci">void pico_sha256_finish(pico_sha256_state_t *state, sha256_result_t *out)</div><div class="ttdoc">Finish the SHA-256 calculation and return the result.</div><div class="ttdef"><b>Definition:</b> sha256.c:167</div></div>
<div class="ttc" id="agroup__pico__sha256_html_ga56a125e51ec616f61082cc63c8599dcb"><div class="ttname"><a href="group__pico__sha256.html#ga56a125e51ec616f61082cc63c8599dcb">pico_sha256_try_start</a></div><div class="ttdeci">int pico_sha256_try_start(pico_sha256_state_t *state, enum sha256_endianness endianness, bool use_dma)</div><div class="ttdoc">Start a SHA-256 calculation returning immediately with an error if the SHA-256 hardware is not availa...</div><div class="ttdef"><b>Definition:</b> sha256.c:33</div></div>
<div class="ttc" id="astructpico__sha256__state_html"><div class="ttname"><a href="structpico__sha256__state.html">pico_sha256_state</a></div><div class="ttdoc">SHA-256 state used by the API.</div><div class="ttdef"><b>Definition:</b> sha256.h:48</div></div>
<div class="ttc" id="aunionsha256__result__t_html"><div class="ttname"><a href="unionsha256__result__t.html">sha256_result_t</a></div><div class="ttdoc">SHA-256 result generated by the API.</div><div class="ttdef"><b>Definition:</b> sha256.h:57</div></div>
</div><!-- fragment --><h2><a class="anchor" id="sha256_example"></a>
Example</h2>
<div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="stdio_8h.html">stdio.h</a>&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div>
<div class="line"><span class="comment">// Include sys/types.h before inttypes.h to work around issue with</span></div>
<div class="line"><span class="comment">// certain versions of GCC and newlib which causes omission of PRIu64</span></div>
<div class="line"><span class="preprocessor">#include &lt;sys/types.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;inttypes.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="stdlib_8h.html">stdlib.h</a>&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="stdlib_8h.html">pico/stdlib.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="rp2__common_2pico__sha256_2include_2pico_2sha256_8h.html">pico/sha256.h</a>&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// This was generated by cmake from sample.txt.inc</span></div>
<div class="line"><span class="preprocessor">#include &quot;sample.txt.inc&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> sha_example() {</div>
<div class="line">    printf(<span class="stringliteral">&quot;Text: %d bytes\n&quot;</span>, <span class="keyword">sizeof</span>(sample_txt) - 1);</div>
<div class="line">    <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0; i &lt; <span class="keyword">sizeof</span>(sample_txt) - 1; i++) {</div>
<div class="line">        <span class="keywordflow">if</span> (i &gt; 0 &amp;&amp; i % 128 == 0) printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line">        putchar(sample_txt[i]);</div>
<div class="line">    }</div>
<div class="line">    printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// Allocate a state object and start the calculation</span></div>
<div class="line">    <a class="code hl_struct" href="structpico__sha256__state.html">pico_sha256_state_t</a> state;</div>
<div class="line">    <span class="keywordtype">int</span> rc = <a class="code hl_function" href="group__pico__sha256.html#gaf8fdc6d7438ffed9fcbc124eb134ae6e">pico_sha256_start_blocking</a>(&amp;state, <a class="code hl_enumvalue" href="group__hardware__sha256.html#ggaaf3089af3667088256de5d352973d5e6a9ed40fa208beaabfc2fc5b8edbd652c1">SHA256_BIG_ENDIAN</a>, <span class="keyword">true</span>); <span class="comment">// using some DMA system resources</span></div>
<div class="line">    hard_assert(rc == <a class="code hl_enumvalue" href="group__pico__base.html#ggaddcb20bc9a61ceff479aa87369a862aaa9b50bcb755cf2e8295de9ffa52b252fd">PICO_OK</a>);</div>
<div class="line">    <a class="code hl_function" href="group__pico__sha256.html#ga9bf8472019386229c3ba3b6d169b9656">pico_sha256_update_blocking</a>(&amp;state, (<span class="keyword">const</span> uint8_t*)sample_txt, <span class="keyword">sizeof</span>(sample_txt) - 1);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// Get the result of the sha256 calculation</span></div>
<div class="line">    <a class="code hl_union" href="unionsha256__result__t.html">sha256_result_t</a> result;</div>
<div class="line">    <a class="code hl_function" href="group__pico__sha256.html#ga4eac54bf699dba0067a22ae3ef5ae121">pico_sha256_finish</a>(&amp;state, &amp;result);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// print resulting sha256 result</span></div>
<div class="line">    printf(<span class="stringliteral">&quot;Result:\n&quot;</span>);</div>
<div class="line">    <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0; i &lt; <a class="code hl_define" href="group__hardware__sha256.html#ga08a18381a4c5a7c9f7d0d1b6550621a3">SHA256_RESULT_BYTES</a>; i++) {</div>
<div class="line">        printf(<span class="stringliteral">&quot;%02x &quot;</span>, result.bytes[i]);</div>
<div class="line">        <span class="keywordflow">if</span> ((i+1) % 16 == 0) printf(<span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// check it&#39;s what we expect from &quot;sha256sum sample.txt&quot;</span></div>
<div class="line">    <span class="keyword">const</span> uint8_t sha_expected[<a class="code hl_define" href="group__hardware__sha256.html#ga08a18381a4c5a7c9f7d0d1b6550621a3">SHA256_RESULT_BYTES</a>] = {</div>
<div class="line">        0x2d, 0x8c, 0x2f, 0x6d, 0x97, 0x8c, 0xa2, 0x17, 0x12, 0xb5, 0xf6, 0xde, 0x36, 0xc9, 0xd3, 0x1f,</div>
<div class="line">        0xa8, 0xe9, 0x6a, 0x4f, 0xa5, 0xd8, 0xff, 0x8b, 0x01, 0x88, 0xdf, 0xb9, 0xe7, 0xc1, 0x71, 0xbb</div>
<div class="line">    };</div>
<div class="line">    hard_assert(memcmp(sha_expected, &amp;result, <a class="code hl_define" href="group__hardware__sha256.html#ga08a18381a4c5a7c9f7d0d1b6550621a3">SHA256_RESULT_BYTES</a>) == 0);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#define BUFFER_SIZE 10000</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// A performance test with a large amount of data</span></div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> nist_test(<span class="keywordtype">bool</span> use_dma) {</div>
<div class="line">    <span class="comment">// nist 3</span></div>
<div class="line">    uint8_t *buffer = malloc(BUFFER_SIZE);</div>
<div class="line">    memset(buffer, 0x61, BUFFER_SIZE);</div>
<div class="line">    <span class="keyword">const</span> uint8_t nist_3_expected[] = { \</div>
<div class="line">        0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,</div>
<div class="line">        0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0 };</div>
<div class="line"> </div>
<div class="line">    uint64_t start = <a class="code hl_function" href="group__hardware__timer.html#ga7bbcd996dbd3dd4adc03a80272fb2fc3">time_us_64</a>();</div>
<div class="line">    <a class="code hl_struct" href="structpico__sha256__state.html">pico_sha256_state_t</a> state;</div>
<div class="line">    <span class="keywordtype">int</span> rc = <a class="code hl_function" href="group__pico__sha256.html#gaf8fdc6d7438ffed9fcbc124eb134ae6e">pico_sha256_start_blocking</a>(&amp;state, <a class="code hl_enumvalue" href="group__hardware__sha256.html#ggaaf3089af3667088256de5d352973d5e6a9ed40fa208beaabfc2fc5b8edbd652c1">SHA256_BIG_ENDIAN</a>, use_dma); <span class="comment">// call start once</span></div>
<div class="line">    hard_assert(rc == <a class="code hl_enumvalue" href="group__pico__base.html#ggaddcb20bc9a61ceff479aa87369a862aaa9b50bcb755cf2e8295de9ffa52b252fd">PICO_OK</a>);</div>
<div class="line">    <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0; i &lt; 1000000; i += BUFFER_SIZE) {</div>
<div class="line">        <a class="code hl_function" href="group__pico__sha256.html#ga9bf8472019386229c3ba3b6d169b9656">pico_sha256_update_blocking</a>(&amp;state, buffer, BUFFER_SIZE); <span class="comment">// call update as many times as required</span></div>
<div class="line">    }</div>
<div class="line">    <a class="code hl_union" href="unionsha256__result__t.html">sha256_result_t</a> result;</div>
<div class="line">    <a class="code hl_function" href="group__pico__sha256.html#ga4eac54bf699dba0067a22ae3ef5ae121">pico_sha256_finish</a>(&amp;state, &amp;result); <span class="comment">// Call finish when done to get the result</span></div>
<div class="line"> </div>
<div class="line">    <span class="comment">// Display the time taken</span></div>
<div class="line">    uint64_t pico_time = <a class="code hl_function" href="group__hardware__timer.html#ga7bbcd996dbd3dd4adc03a80272fb2fc3">time_us_64</a>() - start;</div>
<div class="line">    printf(<span class="stringliteral">&quot;Time for sha256 of 1M bytes %s DMA %&quot;</span>PRIu64<span class="stringliteral">&quot;ms\n&quot;</span>, use_dma ? <span class="stringliteral">&quot;with&quot;</span> : <span class="stringliteral">&quot;without&quot;</span>, pico_time / 1000);</div>
<div class="line">    hard_assert(memcmp(nist_3_expected, result.bytes, <a class="code hl_define" href="group__hardware__sha256.html#ga08a18381a4c5a7c9f7d0d1b6550621a3">SHA256_RESULT_BYTES</a>) == 0);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main() {</div>
<div class="line">    <a class="code hl_function" href="group__pico__stdio.html#ga0e604311fb226dae91ff4eb17a19d67a">stdio_init_all</a>();</div>
<div class="line"> </div>
<div class="line">    sha_example();</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// performance test with and without DMA</span></div>
<div class="line">    nist_test(<span class="keyword">false</span>);</div>
<div class="line">    nist_test(<span class="keyword">true</span>);</div>
<div class="line"> </div>
<div class="line">    printf(<span class="stringliteral">&quot;Success\n&quot;</span>);</div>
<div class="line">}</div>
<div class="ttc" id="agroup__hardware__timer_html_ga7bbcd996dbd3dd4adc03a80272fb2fc3"><div class="ttname"><a href="group__hardware__timer.html#ga7bbcd996dbd3dd4adc03a80272fb2fc3">time_us_64</a></div><div class="ttdeci">uint64_t time_us_64(void)</div><div class="ttdoc">Return the current 64 bit timestamp value in microseconds for the default timer instance.</div><div class="ttdef"><b>Definition:</b> timer.c:125</div></div>
<div class="ttc" id="agroup__pico__sha256_html_ga9bf8472019386229c3ba3b6d169b9656"><div class="ttname"><a href="group__pico__sha256.html#ga9bf8472019386229c3ba3b6d169b9656">pico_sha256_update_blocking</a></div><div class="ttdeci">void pico_sha256_update_blocking(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes)</div><div class="ttdoc">Add byte data to be SHA-256 calculation.</div><div class="ttdef"><b>Definition:</b> sha256.c:141</div></div>
<div class="ttc" id="agroup__pico__sha256_html_gaf8fdc6d7438ffed9fcbc124eb134ae6e"><div class="ttname"><a href="group__pico__sha256.html#gaf8fdc6d7438ffed9fcbc124eb134ae6e">pico_sha256_start_blocking</a></div><div class="ttdeci">static int pico_sha256_start_blocking(pico_sha256_state_t *state, enum sha256_endianness endianness, bool use_dma)</div><div class="ttdoc">Start a SHA-256 calculation, blocking forever waiting until the SHA-256 hardware is available.</div><div class="ttdef"><b>Definition:</b> sha256.h:99</div></div>
<div class="ttc" id="agroup__pico__stdio_html_ga0e604311fb226dae91ff4eb17a19d67a"><div class="ttname"><a href="group__pico__stdio.html#ga0e604311fb226dae91ff4eb17a19d67a">stdio_init_all</a></div><div class="ttdeci">bool stdio_init_all(void)</div><div class="ttdoc">Initialize all of the present standard stdio types that are linked into the binary.</div><div class="ttdef"><b>Definition:</b> stdio.c:200</div></div>
<div class="ttc" id="arp2__common_2pico__sha256_2include_2pico_2sha256_8h_html"><div class="ttname"><a href="rp2__common_2pico__sha256_2include_2pico_2sha256_8h.html">sha256.h</a></div></div>
<div class="ttc" id="astdio_8h_html"><div class="ttname"><a href="stdio_8h.html">stdio.h</a></div></div>
<div class="ttc" id="astdlib_8h_html"><div class="ttname"><a href="stdlib_8h.html">stdlib.h</a></div></div>
</div><!-- fragment --> <h2 class="groupheader">Function Documentation</h2>
<a id="ga4eac54bf699dba0067a22ae3ef5ae121" name="ga4eac54bf699dba0067a22ae3ef5ae121"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga4eac54bf699dba0067a22ae3ef5ae121">&#9670;&nbsp;</a></span>pico_sha256_finish()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void pico_sha256_finish </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *&#160;</td>
          <td class="paramname"><em>state</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="unionsha256__result__t.html">sha256_result_t</a> *&#160;</td>
          <td class="paramname"><em>out</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Finish the SHA-256 calculation and return the result. </p>
<p >Ends the SHA-256 calculation freeing the hardware for use by another caller. You must have called pico_sha256_try_start already.</p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">state</td><td>A pointer to a pico_sha256_state_t instance </td></tr>
    <tr><td class="paramname">out</td><td>The SHA-256 checksum </td></tr>
  </table>
  </dd>
</dl>

</div>
</div>
<a id="gaf8fdc6d7438ffed9fcbc124eb134ae6e" name="gaf8fdc6d7438ffed9fcbc124eb134ae6e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gaf8fdc6d7438ffed9fcbc124eb134ae6e">&#9670;&nbsp;</a></span>pico_sha256_start_blocking()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">static int pico_sha256_start_blocking </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *&#160;</td>
          <td class="paramname"><em>state</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">enum <a class="el" href="group__hardware__sha256.html#gaaf3089af3667088256de5d352973d5e6">sha256_endianness</a>&#160;</td>
          <td class="paramname"><em>endianness</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">bool&#160;</td>
          <td class="paramname"><em>use_dma</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">inline</span><span class="mlabel">static</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">

<p>Start a SHA-256 calculation, blocking forever waiting until the SHA-256 hardware is available. </p>
<p >Initialises the hardware and state ready to start a new SHA-256 calculation. Only one instance can be started at any time.</p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">state</td><td>A pointer to a pico_sha256_state_t instance </td></tr>
    <tr><td class="paramname">endianness</td><td>SHA256_BIG_ENDIAN or SHA256_LITTLE_ENDIAN for data in and data out </td></tr>
    <tr><td class="paramname">use_dma</td><td>Set to true to use DMA internally to copy data to hardware. This is quicker at the expense of hardware DMA resources. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>Returns PICO_OK if the hardware was available for use and the sha256 calculation could be started, otherwise an error is returned </dd></dl>

</div>
</div>
<a id="ga6e6d3e0a3cb87e32a73757bcda88ebc1" name="ga6e6d3e0a3cb87e32a73757bcda88ebc1"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga6e6d3e0a3cb87e32a73757bcda88ebc1">&#9670;&nbsp;</a></span>pico_sha256_start_blocking_until()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int pico_sha256_start_blocking_until </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *&#160;</td>
          <td class="paramname"><em>state</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">enum <a class="el" href="group__hardware__sha256.html#gaaf3089af3667088256de5d352973d5e6">sha256_endianness</a>&#160;</td>
          <td class="paramname"><em>endianness</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">bool&#160;</td>
          <td class="paramname"><em>use_dma</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="group__timestamp.html#ga8b7f3d602cc053f05bacceeacc257ed8">absolute_time_t</a>&#160;</td>
          <td class="paramname"><em>until</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Start a SHA-256 calculation waiting for a defined period for the SHA-256 hardware to be available. </p>
<p >Initialises the hardware and state ready to start a new SHA-256 calculation. Only one instance can be started at any time.</p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">state</td><td>A pointer to a pico_sha256_state_t instance </td></tr>
    <tr><td class="paramname">endianness</td><td>SHA256_BIG_ENDIAN or SHA256_LITTLE_ENDIAN for data in and data out </td></tr>
    <tr><td class="paramname">use_dma</td><td>Set to true to use DMA internally to copy data to hardware. This is quicker at the expense of hardware DMA resources. </td></tr>
    <tr><td class="paramname">until</td><td>How long to wait for the SHA hardware to be available </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>Returns PICO_OK if the hardware was available for use and the sha256 calculation could be started in time, otherwise an error is returned </dd></dl>

</div>
</div>
<a id="ga56a125e51ec616f61082cc63c8599dcb" name="ga56a125e51ec616f61082cc63c8599dcb"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga56a125e51ec616f61082cc63c8599dcb">&#9670;&nbsp;</a></span>pico_sha256_try_start()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int pico_sha256_try_start </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *&#160;</td>
          <td class="paramname"><em>state</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">enum <a class="el" href="group__hardware__sha256.html#gaaf3089af3667088256de5d352973d5e6">sha256_endianness</a>&#160;</td>
          <td class="paramname"><em>endianness</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">bool&#160;</td>
          <td class="paramname"><em>use_dma</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Start a SHA-256 calculation returning immediately with an error if the SHA-256 hardware is not available. </p>
<p >Initialises the hardware and state ready to start a new SHA-256 calculation. Only one instance can be started at any time.</p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">state</td><td>A pointer to a pico_sha256_state_t instance </td></tr>
    <tr><td class="paramname">endianness</td><td>SHA256_BIG_ENDIAN or SHA256_LITTLE_ENDIAN for data in and data out </td></tr>
    <tr><td class="paramname">use_dma</td><td>Set to true to use DMA internally to copy data to hardware. This is quicker at the expense of hardware DMA resources. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>Returns PICO_OK if the hardware was available for use and the sha256 calculation could be started, otherwise an error is returned </dd></dl>

</div>
</div>
<a id="ga43c112e22aea468ce0551c5078e155cd" name="ga43c112e22aea468ce0551c5078e155cd"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga43c112e22aea468ce0551c5078e155cd">&#9670;&nbsp;</a></span>pico_sha256_update()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void pico_sha256_update </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *&#160;</td>
          <td class="paramname"><em>state</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const uint8_t *&#160;</td>
          <td class="paramname"><em>data</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">size_t&#160;</td>
          <td class="paramname"><em>data_size_bytes</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Add byte data to be SHA-256 calculation. </p>
<p >Add byte data to be SHA-256 calculation You may call this as many times as required to add all the data needed. You must have called pico_sha256_try_start (or equivalent) already.</p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">state</td><td>A pointer to a pico_sha256_state_t instance </td></tr>
    <tr><td class="paramname">data</td><td>Pointer to the data to be added to the calculation </td></tr>
    <tr><td class="paramname">data_size_bytes</td><td>Amount of data to add</td></tr>
  </table>
  </dd>
</dl>
<dl class="section note"><dt>Note</dt><dd>This function may return before the copy has completed in which case the data passed to the function must remain valid and unchanged until a further call to pico_sha256_update or pico_sha256_finish. If this is not done, corrupt data may be used for the SHA-256 calculation giving an unexpected result. </dd></dl>

</div>
</div>
<a id="ga9bf8472019386229c3ba3b6d169b9656" name="ga9bf8472019386229c3ba3b6d169b9656"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga9bf8472019386229c3ba3b6d169b9656">&#9670;&nbsp;</a></span>pico_sha256_update_blocking()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void pico_sha256_update_blocking </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__pico__sha256.html#gadb68f9eb89ce1a5b795463cd083a30a0">pico_sha256_state_t</a> *&#160;</td>
          <td class="paramname"><em>state</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const uint8_t *&#160;</td>
          <td class="paramname"><em>data</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">size_t&#160;</td>
          <td class="paramname"><em>data_size_bytes</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Add byte data to be SHA-256 calculation. </p>
<p >Add byte data to be SHA-256 calculation You may call this as many times as required to add all the data needed. You must have called pico_sha256_try_start already.</p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">state</td><td>A pointer to a pico_sha256_state_t instance </td></tr>
    <tr><td class="paramname">data</td><td>Pointer to the data to be added to the calculation </td></tr>
    <tr><td class="paramname">data_size_bytes</td><td>Amount of data to add</td></tr>
  </table>
  </dd>
</dl>
<dl class="section note"><dt>Note</dt><dd>This function will only return when the data passed in is no longer required, so it can be freed or changed on return. </dd></dl>

</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->

	<script src="main.js"></script>
</body>
</html>